package com.example.hp.storeapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.hp.storeapp.Activity.CookDetailActivity;
import com.example.hp.storeapp.Activity.ShopDetailActivity;
import com.example.hp.storeapp.Const;
import com.example.hp.storeapp.R;
import com.example.hp.storeapp.bean.FavorResult;
import com.example.hp.storeapp.bean.NoFavorSResult;
import com.example.hp.storeapp.fragment.ViewHolder.CookFavorListViewHolder;
import com.example.hp.storeapp.fragment.ViewHolder.ShopFavorListViewHolder;
import com.example.hp.storeapp.interf.RetrofitService;
import com.squareup.picasso.Picasso;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by NEUSOFT on 2017/6/6.
 */

public class CookFavoriteListAdapter extends BaseAdapter {
    public CookFavoriteListAdapter(Context context, List data) {
        super(context, data);
    }

    @Override
    RecyclerView.ViewHolder onCreateVH(ViewGroup parent, LayoutInflater layoutInflater, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.item_cook_favorite, parent, false);
        CookFavorListViewHolder viewHolder = new CookFavorListViewHolder(itemView);
        return viewHolder;
    }

    @Override
    void onBindVH(RecyclerView.ViewHolder holder, List data, final int position) {
        CookFavorListViewHolder viewHolder = (CookFavorListViewHolder) holder;
        final FavorResult favorResult =(FavorResult) data.get(position);


        String address = favorResult.getAddress();
        String pic = favorResult.getPic();
        if (!pic.equals("")){
//            imageUrl = " http://172.24.10.130:8080/foodService/"+shopResult.getPic();
            Picasso.with(context).load(pic).into(viewHolder.favorImage);

    }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"菜品详情"+position,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, CookDetailActivity.class);
                intent.putExtra("food_id",favorResult.getFood_id());
                intent.putExtra("shop_id",favorResult.getShop_id());

                context.startActivity(intent);
            }
        });



        viewHolder.cookNameView.setText((CharSequence) favorResult.getFoodname());
        viewHolder.cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消店铺按钮
                doFavor(String.valueOf(favorResult.getUser_id()),String.valueOf(favorResult.getFood_id()));

            }
        });

    }
    private void doFavor(String userId,String food_id) {
        //定义Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //获取retrofit接口
        RetrofitService retrofitService =
                retrofit.create(RetrofitService.class);

        Call<NoFavorSResult> call = retrofitService.userCollectFood(userId, String.valueOf(food_id));
        call.enqueue(new Callback<NoFavorSResult>() {
            @Override
            public void onResponse(Call<NoFavorSResult> call, Response<NoFavorSResult> response) {
                if (response.body().getSuccess().equals("1")) {
                    //操作成功
                    Toast.makeText(context, "取消收藏成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<NoFavorSResult> call, Throwable t) {

            }
        });
    }
}
